/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：CommunityActionDialog.java <br>
 *
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui.meta;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.R;
import lostland.gmud.exv2.community.GmudUser;
import lostland.gmud.exv2.ui.LockedScreen;
import lostland.gmud.exv2.ui.NotificationScreen;

/**
 * 类名：CommunityActionDialog <br>
 * 说明：对社区用户执行操作
 *
 * @author 12548
 */
public abstract class CommunityActionDialog extends Dialog {

	private Window mWindow;

	EditText et;


	/**
	 * @param context
	 */
	public CommunityActionDialog(Context context, String s) {
		super(context);
		mWindow = getWindow();
		mWindow.setBackgroundDrawable(new ColorDrawable(0));
		mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.gravity = Gravity.CENTER;
		lp.dimAmount = 0.0001f;
		mWindow.setAttributes(lp);
		setCanceledOnTouchOutside(true);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mWindow.setContentView(R.layout.dialog_addfriend);

		et = (EditText) findViewById(R.id.etUserName);
		et.setHint(s);
		Button btn = (Button) findViewById(R.id.btnAddFriend);
		btn.setOnClickListener(new View.OnClickListener() {

			                       @Override
			                       public void onClick(View v) {

				                       final boolean noexisting = true;

				                       if (et.getText().toString().equals(GmudUser.getInstance().getUsername())) {
					                       new NotificationScreen("目标不能为自己！").pushToGame();
					                       return;
				                       }

				                       if (noexisting) {
					                       BmobQuery<GmudUser> q = new BmobQuery<>();
					                       q.addWhereEqualTo("username", et.getText().toString());
					                       new LockedScreen(
							                       "正在查询用户......").pushToGame();
					                       CommunityActionDialog.this.cancel();
					                       q.findObjects(Game.getInstance(),
							                       new FindListener<GmudUser>() {

								                       @Override
								                       public void onError(int arg0,
								                                           String arg1) {
									                       Toast.makeText(Game.getInstance(),
											                       "联网失败:" + arg1,
											                       Toast.LENGTH_SHORT)
											                       .show();
									                       Game.getInstance().popScreen();
								                       }

								                       @Override
								                       public void onSuccess(
										                       List<GmudUser> arg0) {

									                       if (arg0.size() == 0) {
										                       Toast.makeText(
												                       Game.getInstance(),
												                       "用户不存在！",
												                       Toast.LENGTH_SHORT)
												                       .show();
										                       Game.getInstance().popScreen();
										                       Game.getInstance().popScreen();
									                       } else {
										                       Game.getInstance().popScreen();
										                       execute(arg0.get(0));
									                       }
								                       }
							                       });
				                       }
				                       cancel();
			                       }
		                       }
		);
	}

	public abstract void execute(GmudUser user);

}
